<template>
  <div class="bg-c-fff mb70 container min-height">
    <h2 class="box-title rel"><a href="/#/index/news-content">内容管理-新闻资讯</a> > {{id | titleFilter}}</h2>
    <ul>
      <li>
        <span>新闻标题：</span>
        <input type="text" class="input-css input-w1" v-model="newsTitle">
      </li>
      <li>
        <span>新闻摘要：</span>
        <textarea class="vt" v-model="abstract"></textarea>
      </li>
      <li>
        <span>新闻预览图片：</span>
        <div class="dib">
          <button class="blue-button xl-btn-css" id="selectBtn">选择文件</button>
          <button class="blue-button xl-btn-css" @click="startUpload">上传</button>
        </div>
        <div class="preview-box">
          <div class="preview" v-if="!newsPicImg">
            <div class="db auto tc">待上传</div>
          </div>
          <div class="preview-img" v-else>
            <img :src="newsPicImg">
          </div>
        </div>
      </li>
      <li>
        <span>新闻内容：</span>
        <UE ref="ue"></UE>
      </li>
      <li>
        <span></span>
        <button class="blue-button xl-btn-css" @click="onSubmit">提交发布</button>
        <button class="xl-btn-css white-button" @click="onCancel">取消</button>
      </li>
    </ul>
  </div>
</template>
<script type="text/javascript">
import UploaderManager from '../components/UploaderManager'
import UE from '../components/UE'
export default {
  components: {
    UE
  },
  data() {
    return {
      newsTitle: '',
      abstract: '',
      uploader: null,
      newsPicImg: '',
      isLoading: false,
      id: '',
      ue: null
    }
  },
  mounted() {
    this.initEditor()
    this.initUploader()
    if (this.$route.query.id) {
      this.id = this.$route.query.id
      this.getContent()
    }
  },
  methods: {
    initEditor() {
      this.ue = window.UE.getEditor('editor')
    },
    getContent() {
      this.$http.get('/sysContent/detail/' + this.id).then(data => {
        if (data.data.httpCode === 200) {
          this.newsTitle = data.data.data.title
          this.abstract = data.data.data.roundup
          this.newsPicImg = data.data.data.image
          this.$refs.ue.setUEContent(data.data.data.content)
        } else {
          this.$toasted.show(data.data.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    },
    onSubmit() {
      if (!this.newsTitle) {
        this.$toasted.show('请输入新闻标题')
        return
      }
      if (!this.abstract) {
        this.$toasted.show('请输入新闻摘要')
        return
      }
      if (!this.newsPicImg) {
        this.$toasted.show('请上传新闻预览图片')
        return
      }
      let content = this.$refs.ue.getUEContent()
      if (!content) {
        this.$toasted.show('请输入新闻内容')
        return
      }
      if (this.isLoading) {
        return
      }
      this.isLoading = true

      this.$http.post('/sysContent/update', {
        title: this.newsTitle,
        roundup: this.abstract,
        image: this.newsPicImg,
        content: content,
        type: 1,
        id: this.id
      }).then(data => {
        this.isLoading = false
        if (data.data.httpCode === 200) {
          this.$router.push('/index/news-content')
          if (this.id) {
            this.$toasted.show('编辑成功')
          } else {
            this.$toasted.show('发布成功')
          }
        } else {
          this.$toasted.show(data.data.msg)
        }
      }).catch(err => {
        console.log(err)
        this.isLoading = false
      })
    },
    onCancel() {
      this.$router.back()
    },
    startUpload() {
      if (this.uploader.startUpload()) {

      } else {
        this.$toasted.show('正在上传文件，请稍后再试')
      }
    },
    initUploader() {
      this.uploader = new UploaderManager('content-photo', 'selectBtn', (url) => {
        this.newsPicImg = url
        this.$toasted.show('上传成功')
      })
    }
  },
  filters: {
    titleFilter(v) {
      if (v) {
        return '编辑新闻'
      } else {
        return '新增新闻'
      }
    }
  }
}

</script>
<style type="text/css" scoped>
.container {
  margin-top: 20px;
}

.min-height {
  min-height: 507px;
}

.box-title a {
  color: #a5a5a5;
}

ul {
  padding: 0 0 70px 190px;
}

ul li {
  padding: 10px 0;
}

ul li span {
  width: 194px;
  display: inline-block;
  text-align: right;
  color: #666666;
  margin-right: 20px;
  vertical-align: top;
  margin-top: 5px;
}

textarea {
  width: 350px;
  height: 140px;
  padding: 10px;
}

button {
  margin-right: 45px;
}

input[type="file"] {
  left: -150px;
}

.preview-box {
  margin-left: 217px;
  margin-top: 10px;
}

.preview {
  width: 120px;
  height: 120px;
  border: 1px solid #cccccc;
  margin-right: 70px;
}

.preview div {
  width: 100px;
  height: 100px;
  border-radius: 100%;
  border: 1px solid #cccccc;
  margin-top: 10px;
  line-height: 100px;
  color: #ccc;
}

.preview-img {
  width: 500px;
}

.preview-img img {
  width: 100%;
  height: auto;
}

</style>
